// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 60 : TYPE_EGGMAN

// Function declarations
reserve function Eggman_Function129

// Static Values

// Tables

function Eggman_Function129
	if object.value6 == 0
		PlaySfx(SfxName[Helicopter], 0)
	end if
	object.value6++
	object.value6 &= 31
end function


event ObjectMain
	switch object.state
	case 0
		temp0 = object.ixpos
		temp0 -= 200
		stage.newXBoundary1 = temp0
		temp0 = object.ixpos
		temp0 += 200
		stage.newXBoundary2 = temp0
		if object[0].xpos > object.xpos
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object[+1].type = TypeName[Eggman Car]
			object[+1].xpos = object.xpos
			object[+1].xpos += 0x1590000
			object[+1].ypos = object.ypos
			object[+1].value2 = object.xpos
			object[+1].value2 -= 0x1180000
			temp0 = object.ixpos
			temp0 -= 200
			temp0 += screen.xcenter
			if screen.cameraX < temp0
				temp0 -= screen.cameraX
				temp0 <<= 16
				object[+1].value2 -= temp0
			end if
			object[+1].value3 = object.xpos
			object[+1].value3 += 0x1500000
			object[+1].priority = PRIORITY_ACTIVE
			object[+2].type = TypeName[Eggman Drill]
			object[+2].priority = PRIORITY_ACTIVE
			object[-1].type = TypeName[Eggman Wheel]
			object[-1].value1 = -0x2C0000
			object[-1].value2 = object[+1].entityPos
			object[-1].priority = PRIORITY_ACTIVE
			object[+3].type = TypeName[Eggman Wheel]
			object[+3].value1 = -0xC0000
			object[+3].value2 = object[+1].entityPos
			object[+3].priority = PRIORITY_ACTIVE
			object[+4].type = TypeName[Eggman Wheel]
			object[+4].value1 = 0x1C0000
			object[+4].value2 = object[+1].entityPos
			object[+4].priority = PRIORITY_ACTIVE
			object.xpos += 0x1580000
			object.ypos -= 0x13F0000
			object.priority = PRIORITY_ACTIVE
			object.state++
		end if
		break
	case 1
		CallFunction(Eggman_Function129)
		object.xpos -= 0x10000
		object.ypos += 0x10000
		object.value0++
		if object.value0 == 320
			object.value3 = 1
			object.value4 = 0
			object.value0 = 0
			object.state++
		end if
		break
	case 2
		break
	case 3
		object.value0++
		if object.value0 == 24
			object.value0 = 0
			object.value8 = 8
			object.state++
			object[+1].xvel = -0x20000
			object[+1].state++
		end if
		break
	case 4
		break
	case 5
		object.value0++
		if object.value0 == 180
			object.value0 = 0
			object.value3 = 4
			object.animation = 5
			object.value2 = false
			object.state++
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break
	case 6
		break
	case 7
		CallFunction(Eggman_Function129)
		object.ypos -= 0x10000
		object.value0++
		if object.value0 == 96
			object.value0 = 0
			object.direction = FLIP_X
			object.animation = 6
			object.state++
		end if
		break
	case 8
		if object.outOfBounds == false
			CallFunction(Eggman_Function129)
		end if
		object.xpos += 0x60000
		temp0 = stage.newXBoundary2
		temp0 += 128
		temp0 <<= 16
		if object.xpos >= temp0
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.value8 != 0
		if object.value9 > 0
			object.value9--
			GetBit(temp0, object.value9, 0)
			if temp0 == true // Palette flashing when hit
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value9 == 0 // Invulnerable?
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
					if currentPlayer == 0
						object.value10 = 1
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 24, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == true
					CallFunction(PlayerObject_CheckHit)
					if checkResult == true
						object.value8--
						if object.value8 == 0
							player.score += 1000
							object.animation = 4
							object.animationTimer = 0
							object.value2 = true
							object.state++
							object[+1].state++
							object[+1].yvel = -0x18000
							object[+1].ypos = object[+1].value1
							if object[+2].state == 0
								object[+2].state = 2
								if object.direction == FLIP_NONE
									object[+2].xvel = -0x30000
								else
									object[+2].xvel = 0x30000
								end if
							end if
						else
							object.animation = 3
							object.animationTimer = 0
							object.value9 = 32
							PlaySfx(SfxName[Boss Hit], 0)
						end if
					end if
				end if
			end if
		next
	end if
	if object.state != 8
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value2 == true
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 96)
			temp0 -= 48
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	if object.animation != 0
		switch object.value3
		case 0
			temp0 = object.value4
			temp0 >>= 1
			temp0 += 12
			object.value4++
			object.value4 &= 3
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
			break
		case 1
			temp0 = object.value4
			temp0 /= 6
			temp0 += 8
			object.value4++
			if object.value4 == 24
				object.value4 = 0
				object.value3 = 2
			end if
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
			break
		case 2
			temp0 = object.value4
			temp0 <<= 16
			temp0 += object.ypos
			DrawSpriteFX(7, FX_FLIP, object.xpos, temp0)
			object.value4++
			if object.value4 == 12
				object.state++
				object.value3 = 3
			end if
			break
		case 4
			object.value4--
			if object.value4 > 0
				temp0 = object.value4
			else
				temp0 = 0
			end if
			temp0 <<= 16
			temp0 += object.ypos
			DrawSpriteFX(7, FX_FLIP, object.xpos, temp0)
			if object.value4 == -12
				object.value4 = 24
				object.value3 = 5
			end if
			break
		case 5
			object.value4--
			temp0 = object.value4
			temp0 /= 6
			temp0 += 8
			if object.value4 == 0
				object.value3 = 0
				object.state++
			end if
			DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
			break
		end switch
		DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
	end if
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 >>= 3
		if saveRAM[495] == 0
			object.animationTimer++
			object.animationTimer %= 15
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 &= 1
		if temp0 == 1
			DrawSpriteFX(14, FX_FLIP, object.xpos, object.ypos)
		end if
		break
	end switch
end event


event ObjectStartup

	// Check if in EHZ
	CheckCurrentStageFolder("Zone01")
	if checkResult == true

		// Load EHZ stuff
			if saveRAM[495] == 0
				LoadSpriteSheet("EHZ/Objects.gif")
			else
				LoadSpriteSheet("EHZ/ObjectsE.gif")
			endif
		SpriteFrame(-28, -28, 60, 20, 70, 155)
		SpriteFrame(-28, -28, 60, 20, 131, 155)
		SpriteFrame(-28, -28, 60, 20, 70, 176)
		SpriteFrame(-28, -28, 60, 20, 131, 176)
		SpriteFrame(-28, -28, 60, 20, 70, 197)
		SpriteFrame(-28, -28, 60, 20, 131, 197)
		SpriteFrame(-32, -8, 64, 29, 0, 209)
		SpriteFrame(2, -40, 16, 16, 65, 239)
		SpriteFrame(-62, -40, 144, 16, 1, 239)
		SpriteFrame(-46, -40, 112, 16, 17, 239)
		SpriteFrame(-30, -40, 80, 16, 33, 239)
		SpriteFrame(-14, -40, 48, 16, 49, 239)
		SpriteFrame(2, -40, 80, 16, 65, 222)
		SpriteFrame(-62, -40, 79, 16, 1, 239)
		SpriteFrame(-3, -28, 11, 8, 27, 53)

		// Erase the boss in VS mode
		if options.vsMode == true
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-28, -28, 60, 20, 1, 1)
		SpriteFrame(-28, -28, 60, 20, 62, 1)
		SpriteFrame(-28, -28, 60, 20, 1, 22)
		SpriteFrame(-28, -28, 60, 20, 62, 22)
		SpriteFrame(-28, -28, 60, 20, 1, 43)
		SpriteFrame(-28, -28, 60, 20, 62, 43)
		SpriteFrame(-32, -8, 64, 29, 415, 170)
		SpriteFrame(2, -40, 16, 16, 169, 91)
		SpriteFrame(-62, -40, 144, 16, 105, 91)
		SpriteFrame(-46, -40, 112, 16, 121, 91)
		SpriteFrame(-30, -40, 80, 16, 137, 91)
		SpriteFrame(-14, -40, 48, 16, 153, 91)
		SpriteFrame(2, -40, 80, 16, 42, 64)
		SpriteFrame(-62, -40, 79, 16, 105, 91)
		SpriteFrame(-3, -28, 11, 8, 45, 88)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
